package leetcode.weekly.week330;

import java.util.Arrays;

//Solution3Test
public class Solution3 {
	public long putMarbles(int[] weights, int k) {
		int len = weights.length;
		int[] sum = new int[len - 1];
		for (int i = 1, j = 0; i < len; i++, j++) {
			sum[j] = weights[i] + weights[i - 1];
		}
		long ans = 0;
		Arrays.sort(sum);
		for (int i = 1; i < k; i++) {
			ans += sum[sum.length - i] - sum[i - 1];
		}
		return ans;
	}

}
